package com.heima.wemedia.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDTO;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vos.WmUserVO;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Override
    public ResponseResult findByName(String name) {
        //根据用户名查询自媒体用户，没有用户名为空就查询所有
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getName,name);
        WmUser wmUser = this.getOne(wrapper);
        return ResponseResult.okResult(wmUser);
    }

    @Override
    public ResponseResult saveWmUser(WmUser wmUser) {
        if (wmUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
         wmUser.setCreatedTime(new Date());
        this.save(wmUser);//创建自媒体账号
        return ResponseResult.okResult(wmUser);
    }
    //自媒体用户登录
    @Override
    public ResponseResult<WmUser> login(WmUserDTO wmUserDTO) {
        //1.参数校验
        if (StringUtils.isBlank(wmUserDTO.getName()) || StringUtils.isBlank(wmUserDTO.getPassword()) ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码不能为空");
        }
        //2.根据用户名查询用户，用户名和密码是否都正确
          //2.1根据用户名查询自媒体用户
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmUser::getName,wmUserDTO.getName());
        WmUser one = this.getOne(wrapper);
        if (one == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"不存在该账号");

        }
        //自媒体账号状态：0 暂时不可用，1--永久不可用，9正常可用
        if (one.getStatus().intValue()!=9){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_STATUS_ERROR,"该账号处于不可用状态");

        }
        //判断密码是否正确，密码使用加盐加密后比对
        String password = wmUserDTO.getPassword();
        password= DigestUtils.md5DigestAsHex((password+one.getSalt()).getBytes()) ;
        if (!password.equals(one.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
        }
        //用户登录成功颁发令牌
        String token = AppJwtUtil.getToken(one.getId().longValue());
        //返回令牌和数据封装到map
        WmUserVO wmUserVO = new WmUserVO();
        BeanUtils.copyProperties(one,wmUserVO);
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",wmUserVO);
        return ResponseResult.okResult(map);
    }
}
